<template>
  <div class="app-container">
    <div class="filter-container">
      审核状态:
      <el-select v-model="listQuery.status" clearable placeholder="审核状态" label="审核状态" class="mring10 filter-item">
        <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>
   
      商品评分:
      <el-select v-model="listQuery.goods_mark" clearable placeholder="商品评分" class="mring10 filter-item">
        <el-option v-for="item in remarkType" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>

      订单号:
      <el-input v-model="listQuery.oid" label="订单号" placeholder="订单号" style="width: 200px;" class="filter-item mring10"
        @keyup.enter.native="handleFilter" suffix-icon="el-icon-search"/>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter" size="small"> 搜索</el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" fit stripe highlight-current-row
      style="width: 100%;margin-top: 1.125rem" row-key="id">
      <el-table-column label="商品信息" prop="name" min-width="150">
        <template slot-scope="{row}">
          <img v-if="row.goods.img" :src="row.goods.img" class="avatar"
            style="float:left;margin-right:5px;max-width: 80px;max-height: 80px;padding: 1px;border:1px solid #ddd;"></img>
          <p class="goods-name">{{ row.goods.name }}</p>
        </template>
      </el-table-column>
      <el-table-column label="评分">
        <template slot-scope="{row}">
          {{ row.goods_mark == 0 ? '好评' : (row.goods_mark == 1 ? '中评' : '差评') }}
        </template>
      </el-table-column>
      <el-table-column label="订单号" prop="oid"></el-table-column>
      <el-table-column label="会员名" prop="nickname"></el-table-column>
      <el-table-column label="评价内容" prop="content"></el-table-column>
      <el-table-column label="回复内容" prop="reply_content"></el-table-column>
      <el-table-column label="支付时间">
        <template slot-scope="{row}">
          {{ row.purchase_time | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}
        </template>
      </el-table-column>
      <el-table-column label="评价时间" prop="create_time" align="center">
        <template slot-scope="{row}">
          {{ row.create_time | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}
        </template>
      </el-table-column>
      <el-table-column label="状态" >
         <template slot-scope="{row}">
          {{ row.status == 0 ? '待审核' : (row.status == 1 ? '已审核' : '已驳回') }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button-group v-if="row.status == 0">
            <el-button class="btn-pass btn" type="text" @click="pass(row.id)">通过</el-button>
            <el-button class="btn" type="text" @click="unpass(row.id)">驳回</el-button>
          </el-button-group>
          <el-button-group v-else-if="row.status == 1">
            <el-button v-if="row.reply_content" class=" btn" type="text" @click="replay(row.id)">修改回复
            </el-button>
            <el-button v-else class="btn" type="text" @click="replay(row.id)">回复</el-button>
          </el-button-group>
          <el-button-group v-else>
            
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
    <pagination hide-on-single-page v-show="total > 0" :total="total" :page.sync="listQuery.page"
      :limit.sync="listQuery.size" @pagination="getList" />
  </div>
</template>
<script>
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination'
import { fetchList, replay, pass, unpass } from '@/api/goods/goodsComment'

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      list: [],
      listLoading: false,
      total: 0,
      listQuery: {
        size: 10,
        page: 1
      },
      statusOptions: [
        {
          value: 0,
          label: '未审核'
        },
        {
          value: 1,
          label: '已审核'
        },
        {
          value: 2,
          label: '已驳回'
        },
        {
          value: 3,
          label: '自动审核'
        }
      ],
      remarkType: [
        {
          value: 0,
          label: '好评'
        },
        {
          value: 1,
          label: '中评'
        },
        {
          value: 2,
          label: '差评'
        }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    pass(id) {

      this.$confirm('通过审核后，会展示在商品评论区', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        pass({ id: id }).then(response => {
          this.$message({
            type: 'success',
            message: '已通过审核'
          });

          this.getList()
        })

      });
    },
    unpass(id) {
      this.$confirm('驳回后，不会展示在商品评论区', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        unpass({ id: id }).then(response => {
          this.$message({
            type: 'success',
            message: '已驳回'
          });
          this.getList()
        })
      });
    },
    replay(id) {
      this.$prompt('请输入回复内容', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({ value }) => {
        replay({ id: id, content: value }).then(response => {
          this.$message({
            type: 'success',
            message: '已回复'
          });
          this.getList()
        })
      }).catch();
    }
  }
}

</script>
<style type="text/css">
.avatar,
.avatar-uploader-icon {
  width: 90px;
  height: 90px;
  padding: 1px;
  border: 1px solid #ccc;
  text-align: center;
  line-height: 90px;
}

.filter-container {
  background: #FFFFFF;
  padding: 0.625rem 1.125rem;
}

table td {
  font-size: 12px;
  color: #8c8c8c;
}

.goods-name {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  line-height: 18px
}

.btn {
  padding: 0 10px;
  font-size: 12px;
}

.btn-pass {
  border-right: 1px solid #c0c4cc
}
  .filter-container{font-size: 12px;}
  .filter-item input[type='text'] {
  height: 32px;
  line-height: 32px;
  font-size: 12px;
}

.mring10 {
  margin-right: 10px;
}
</style>
